import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import java.util.Set;

public class Run   {
    ChromeDriver driver = new ChromeDriver();
    //选择框
    public void select() throws InterruptedException {
        driver.get("file:///C:/Users/WHITEW~1/AppData/Local/Temp/k52zip/%7BA1A44BF7-1C5B-47A3-8004-359AB3C39682%7D/drop_down.html");
        WebElement element= driver.findElement(By.cssSelector("#ShippingMethod"));
        Select select=new Select(element);
        //通过下标进行选择
        select.selectByIndex(1);
        Thread.sleep(3000);
        //通过属性来选择
        select.selectByValue("9.25");
        Thread.sleep(3000);
        //
    }
    //模拟输盘
    public  void keyBoard() throws InterruptedException {
        driver.get("file:///C:/Users/WHITEW~1/AppData/Local/Temp/k52zip/%7B2AA095E7-7C8B-4E37-97C4-0047DAD92F8C%7D/send.html");
        driver.findElement(By.cssSelector("body > input[type=button]")).click();
        Thread.sleep(1000);
        Alert alert=driver.switchTo().alert();
        alert.sendKeys("明年有一个好工作");
        Thread.sleep(2000);
        alert.accept();
        Thread.sleep(2000);
    }
    //模拟鼠标
    public  void  mouse() throws InterruptedException {
        driver.get("file:///C:/Users/WHITEW~1/AppData/Local/Temp/k52zip/%7BBA9FE2B2-B66D-46F9-8440-1A4F3D6E5AE3%7D/level_locate.html");
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("body > div:nth-child(2) > div > div > a")).click();
        WebElement elem=driver.findElement(By.cssSelector("#dropdown1 > li:nth-child(3) > a"));
        Actions actions= new Actions(driver);
        actions.clickAndHold(elem).perform();
        Thread.sleep(3000);
        actions.click(elem).perform();
        Thread.sleep(3000);
    }
    //弹窗
    public  void alter()throws InterruptedException{
        //警告弹窗
        //driver.get("file:///C:/Users/WHITEW~1/AppData/Local/Temp/k52zip/%7B3BDEC748-A114-4660-9F45-4745C82D70C0%7D/alert.html#");
        //driver.findElement(By.cssSelector("#tooltip")).click();
        //切换到弹窗上
        //Alert alert=driver.switchTo().alert();
        //Thread.sleep(2000);
        //接受
        //alert.accept();
        //Thread.sleep(2000);
        //提示弹窗
        driver.get("file:///C:/Users/WHITEW~1/AppData/Local/Temp/k52zip/%7B9C6C8CFE-AA98-401B-9628-962CBE08F42E%7D/send.html");
        driver.findElement(By.cssSelector("body > input[type=button]")).click();
        //切换到弹窗上
        Alert alert=driver.switchTo().alert();
        alert.sendKeys("明年会找到好的实习和工作");
        Thread.sleep(3000);
        alert.accept();
        Thread.sleep(2000);
    }
    //导航
    public  void navigate()throws InterruptedException{
    driver.get("https://tool.lu/");
    driver.findElement(By.cssSelector("#page > div:nth-child(1) > div.tabs-wrap > ul > li:nth-child(7) > a")).click();
    Thread.sleep(3000);
    //后退
        driver.navigate().back();
        Thread.sleep(3000);
        //前进
        driver.navigate().forward();
        Thread.sleep(3000);
    }

    //打印当前标题和路径
    public void window() throws InterruptedException  {
        driver.get("https://www.baidu.com");
        //获取当前页面的句柄
        String window1 = driver.getWindowHandle();
        System.out.println(window1);
        //找到新闻并点击
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        Set<String> set = driver.getWindowHandles();
        for (String window : set) {
            if (window != window1) {
                //跳转到该页面
                driver.switchTo().window(window);
            }
        }
        //调整窗口大小
        //窗口最大化
        driver.manage().window().maximize();
        Thread.sleep(3000);
        //窗口最小化
        driver.manage().window().minimize();
        Thread.sleep(3000);
        //调整窗口任意大小
        driver.manage().window().setSize(new Dimension(1000,800));
        Thread.sleep(3000);
        //打印当前页面的标题和路径
        String title = driver.getTitle();
        String url = driver.getCurrentUrl();
        System.out.println("页面标题：" + title + " 路径是：" + url);
    }

    public void Title() {
        driver.get("https://www.baidu.com");
        //打印标题
        String title = driver.getTitle();
        System.out.println(title);
        //打印当前路径
        String url = driver.getCurrentUrl();
        System.out.println(url);
    }

    //关闭浏览器
    public void end() throws InterruptedException {
        Thread.sleep(3000);
        driver.quit();
    }
}
